package com.guohu.controller;

import com.bean.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guohu.service.GuoHuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class GuoHuController {
    @Autowired
    GuoHuService guoHuService;

    @RequestMapping("guohu.do")
    public void get(HttpServletResponse response) throws IOException {
        response.sendRedirect("newhome.html");

    }

    @RequestMapping("guohu_use.do")//过户单管理

    public String check(HttpServletRequest request){
        List<WmsTTransfer> wmsTTransfers = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(10);
        list.add(15);
       request.getSession().setAttribute("list",list);
        System.out.println(list);
        System.out.println(list);
        int pageNum = 1;
        int pageRows = 3;
        PageHelper.startPage(pageNum,pageRows);
        PageInfo<WmsTTransfer> pageInfo = new PageInfo<>(wmsTTransfers);
        request.getSession().setAttribute("pageInfo",pageInfo);
        WmsTTransfer wmsTTransfer = new WmsTTransfer();
        request.getSession().setAttribute("wmsTTransfer",wmsTTransfer);
        return "guohu/guohu_use";
    }

    @RequestMapping("guohu_manger.do")//过户办理

    public String manger(HttpServletRequest request){
        String uuid1 = UUID.randomUUID().toString().replace("-","");
        String uuid = uuid1.substring(0,15);
        request.setAttribute("uuid",uuid);
        Date nowdate = new Date();
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String ordertime = simpleDateFormat.format(nowdate);
        return "guohu/guohu_manger";
    }

    @RequestMapping("guohu_getInUser.do")
    @ResponseBody
    public List<WmsTCustomer> getInUser(HttpServletRequest request){
        List<WmsTCustomer> wmsTCustomer = guoHuService.get();
        return wmsTCustomer;
    }

    @RequestMapping("guohu_getOutUser.do")
    @ResponseBody
    public List<WmsTCustomer> getOutUser(HttpServletRequest request){
        List<WmsTCustomer> wmsTCustomer = guoHuService.getOutUser();
        return wmsTCustomer;
    }

    //选货
    @RequestMapping("guohu_choose.do")
    public String choose(HttpServletRequest request,HttpServletResponse response,String transferVender) throws IOException {
        if(transferVender==null||transferVender.equals("")||transferVender==""){
            response.setCharacterEncoding("gbk");
            response.getWriter().print("<script>alert('卖家信息为空不能选货')</script>");
            String uuid1 = UUID.randomUUID().toString().replace("-","");
            String uuid = uuid1.substring(0,15);
            request.setAttribute("uuid",uuid);
            return "guohu/guohu_manger";
        }else {
            System.out.println(transferVender+"卖家");
            String[] value1 = transferVender.split("-");
            String custid = value1[0];
            String name = value1[1];
            request.setAttribute("name",name);
            List<WmsTBillDetails> wmsTBillDetails = guoHuService.getInformation(custid);
            request.setAttribute("wmsTBillDetails",wmsTBillDetails);
            return "guohu/guohu_choose";
        }

    }

    //选货完毕返回过户办理主页面
    @RequestMapping("guohu_reMan.do")
    public String returnMan(HttpServletRequest request,String billidd,HttpServletResponse response) throws IOException {
        if(billidd!=null) {

            String[] value1 = billidd.split(",");
            String billid = value1[0];
            String name1 = guoHuService.getCustmername(billid);
            String custid = guoHuService.getCustomerid(billid);
            String name = custid + "-" + name1;
            request.setAttribute("name", name);
            String uuid1 = UUID.randomUUID().toString().replace("-", "");
            String uuid = uuid1.substring(0, 15);
            request.setAttribute("uuid", uuid);
            List<WmsTBillDetails> wmsTBillDetails = guoHuService.getInformation(custid);
            request.setAttribute("wmsTBillDetails", wmsTBillDetails);
            //获得托盘个数
            String customerId = custid;
            int trayNum = guoHuService.getTray(customerId);
            request.setAttribute("trayNum", trayNum);
            double num1 = 0;
            //获得总重量
            for (int i = 0; i < wmsTBillDetails.size(); i++) {
                double num = wmsTBillDetails.get(i).getDetailsGoodsSpec() * wmsTBillDetails.get(i).getCurrentGoodsNum();
                num1 = num1 + num;
            }
            request.setAttribute("Num", num1);
            //获得存储费用
            Double storeNum = guoHuService.getResultNum(customerId);
            request.setAttribute("storeNum", storeNum);
            //获得其他费用
            Double otherNum = guoHuService.getOtherNum(customerId);
            request.setAttribute("otherNum", otherNum);
            //获得总费用
            Double allNum = storeNum + otherNum;
            request.setAttribute("allNum", allNum);
            //获取件数下个需求用
            int proNum = guoHuService.getProNum(customerId);
            request.setAttribute("proNum", proNum);
            return "guohu/guohu_manger";
        }else {
            response.setCharacterEncoding("gbk");
            response.getWriter().print("<script>alert('该用户厂库商品异常，请稍后再试')</script>");
            return "guohu/guohu_manger";
        }
    }

    //结算按钮
    @RequestMapping("guohu_getResult.do")
    public String getResult(HttpServletResponse response, WmsTTransfer wmsTTransfer,String transferVender,int proNum,WmsTBill wmsTBill,String transferBuyer) throws IOException {
        //判断买家，卖家信息是否都为非空
        //日期格式
        Date nowdate = new Date();
        String format = "yyyy-MM-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String ordertime = simpleDateFormat.format(nowdate);
        //更改姓名格式
        String[] value1 = wmsTTransfer.getTransferVender().split("-");
        String name = value1[1];
        String[] value2 = wmsTTransfer.getTransferBuyer().split("-");
        String name1 = value2[1];
        wmsTTransfer.setTransferVender(name);
        wmsTTransfer.setTransferBuyer(name1);
        wmsTTransfer.setTransferEditTime(ordertime);
        wmsTTransfer.setTransferDate(ordertime);
        wmsTTransfer.setTransferIsDel(0);


        //插入wmsTTransfer表
        /*guoHuService.insertRtan(wmsTTransfer);*/
        //再次获取卖家id
        String [] id1 = transferVender.split("-");
        String customerID = id1[0];
        //再次获取买家id
        String [] id2 = transferBuyer.split("-");
        String customerID1 = id2[0];
        //根据id获取所有detailsID
        List<String> detailsId =  guoHuService.getdetailsID(customerID);
        //将集合转为json易于对象内存储
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(detailsId);
       //将json转为java集合
        List<String> list = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, String.class));
        //获取transferId
        String transferId = wmsTTransfer.getTransferId();
        //封装好一个bean准备扎入
        WmsTTransDetails wmsTTransDetails = new WmsTTransDetails();
        String uuid1 = UUID.randomUUID().toString().replace("-","");
        String uuid = uuid1.substring(0,10);
        wmsTTransDetails.setTdetailsId(uuid);
        wmsTTransDetails.setTransferId(transferId);
        wmsTTransDetails.setDetailsId(json);
        wmsTTransDetails.setTdetailsGoodsNum(guoHuService.getProNum(customerID));
        wmsTTransDetails.setTdetailsEditTime(ordertime);
        wmsTTransDetails.setTdetailsIsDel(0);
        guoHuService.insertDetails(wmsTTransDetails);
        //卖家过户完后的商品信息设置为已删除
       /* WmsTBill wmsTBill1 = new WmsTBill();
        String uuid2 = UUID.randomUUID().toString().replace("-","");
        String uuid3 = uuid1.substring(0,10);
        wmsTBill1.setBillId(uuid3);
        wmsTBill1.setCustomerId(customerID);
        wmsTBill1.setBillType("2");
        wmsTBill1.setBillDate(ordertime);
        wmsTBill1.setBillFeeStore(wmsTBill.getBillFeeStore());
        wmsTBill1.setBillFeeOther(wmsTBill.getBillFeeOther());
        wmsTBill1.setBillPayable(wmsTBill.getBillPayable());
        wmsTBill1.setBillPaid(wmsTBill.getBillPaid());
        wmsTBill1.setBillEditTime(ordertime);
        wmsTBill.setBillIsDel(0);
        guoHuService.insertBill(wmsTBill1);*/


        //买家信息插入bill
        WmsTBill wmsTBill2 = new WmsTBill();
        String uuid4 = UUID.randomUUID().toString().replace("-","");
        String uuid5 = uuid4.substring(0,10);
        System.out.println(uuid5+"我是uuid");
        wmsTBill2.setBillId(uuid5);
        wmsTBill2.setCustomerId(customerID1);
        wmsTBill2.setBillType("0");
        wmsTBill2.setBillDate(ordertime);
        wmsTBill2.setBillFeeStore(wmsTBill.getBillFeeStore());
        wmsTBill2.setBillFeeOther(wmsTBill.getBillFeeOther());
        wmsTBill2.setBillPayable(wmsTBill.getBillPayable());
        wmsTBill2.setBillPaid(wmsTBill.getBillPaid());
        wmsTBill2.setBillEditTime(ordertime);
        wmsTBill2.setBillIsDel(0);
        guoHuService.insertBill1(wmsTBill2);


        List<String> billids =  guoHuService.getOutUserBillid(customerID);
        System.out.println(billids);
        for (String billid:billids
        ) {
            guoHuService.updateDetailsDel(billid);
            guoHuService.updateBillisDel(billid);
        }


        response.setCharacterEncoding("gbk");
        response.getWriter().print("<script>alert('操作成功')</script>");
        return "guohu/guohu_manger";
    }

    //分页
    @RequestMapping("guohu_fenye.do")
    public String getFenYe( WmsTTransfer wmsTTransfer,HttpServletRequest request,String pageNum2,String pageRows2){
        System.out.println(pageNum2+"1111");
        System.out.println("我"+pageRows2+"爱你");
        int pageNum = 1;
        if(pageNum2!=null){
            int a = Integer.parseInt(pageNum2);
             pageNum = a;
        }
        int pageRows = 5;
        if(pageRows2!=null){
            int b = Integer.parseInt(pageRows2);
            pageRows = b;
        }
        PageHelper.startPage(pageNum,pageRows);
        System.out.println("我是"+pageRows);
        List<WmsTTransfer> wmsTTransfers = guoHuService.getWmsInfor(wmsTTransfer);
        request.getSession().setAttribute("wmsTTransfer",wmsTTransfer);
        PageInfo<WmsTTransfer> pageInfo = new PageInfo<>(wmsTTransfers);
        request.getSession().setAttribute("pageInfo",pageInfo);
        request.getSession().setAttribute("pageRows",pageRows);
        System.out.println(wmsTTransfers);
        return "guohu/guohu_use";
    }
}
